package persentation.presentTeacher;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;









import vo.courseVO.CourseVO;
import vo.userVO.StudentVO;
import businesslogic.chooseCourseBl.ChooseCourseController;
import businesslogic.courseBl.CourseController;
import businesslogic.userBl.UniversityDean;

public class AddCourseInfoPanel extends JPanel{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ArrayList<CourseVO> courseVOs;
	private JTable table;
	private JButton addButton;
	private JScrollPane scrollPane;
	private MyTabbedPane tabbedPane;
	private CourseController controller;
	/**
	 * Create the panel.
	 */
	
	public AddCourseInfoPanel( CourseController courseController) {

		
		
		setSize(800, 600);
		setLayout(null);
		
		this.controller=courseController;
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 10, 780, 404);
		this.add(scrollPane);
		
	    
		addButton=new JButton("课程信息");
		addButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				
				CourseVO courseVO=courseVOs.get(table.getSelectedRow());
				
				String courseID=courseVO.getCourseId();
				

			
				FillCourseInfoPanel fillCourseInfoPanel=new FillCourseInfoPanel(courseVO);
				
				System.out.println((fillCourseInfoPanel==null));
				System.out.println("____________________________");
				System.out.println((courseVO==null));
				System.out.println("++++++++++++++++++++++++++=+");
				System.out.println((tabbedPane==null));
				if(tabbedPane.indexOfTab(courseVO.getCourseName()+" 课程信息")!=-1){
					tabbedPane.removeOne(tabbedPane.indexOfTab(courseVO.getCourseName()+" 课程信息"));
					tabbedPane.remove(tabbedPane.indexOfTab(courseVO.getCourseName()+" 课程信息"));

					tabbedPane.updateUI();

				}
				FillCourseInfoPanel fillInfoPanel=new FillCourseInfoPanel(courseVO);
			
				fillInfoPanel.setTabPane(tabbedPane);
				tabbedPane.addOne(courseVO.getCourseName()+" 课程信息",fillInfoPanel);
			
				table.clearSelection() ;
				addButton.setEnabled(false);
				
			}
		});
		
		addButton.setEnabled(false);
		addButton.setBounds(300, 452, 174, 47);
		
		addButton.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		add(addButton);
		
		
		
		table = new JTable(){
			
		};
		
		table.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		table.setModel(new DefaultTableModel(
			new Object[][] {
				
				
				
				
			},
			new String[] {
				"课程编号", "课程名称", "上课院系", "上课时间", "学分"
			}
		) {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			@SuppressWarnings("rawtypes")
			Class[] columnTypes = new Class[] {
				String.class, String.class, String.class, String.class, String.class
			};
			@SuppressWarnings({ "unchecked", "rawtypes" })
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
			
			public boolean isCellEditable(int row, int column)
			{
				
				
				return false;
			}
		});
		table.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				if(table.getSelectedRow()!=-1)
					addButton.setEnabled(true);
				
			}
		});
		table.setBounds(10, 10, 780, 404);
		scrollPane.setViewportView(table);
		table.setForeground(new Color(0, 0, 0));
		table.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
	
		table.add(table.getTableHeader());
		
		table.getTableHeader().setReorderingAllowed(false);
		table.getTableHeader().setResizingAllowed(false);
		
		
		fillTable();

	}
	
	public void fillTable(){
		DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
		 tableModel.setRowCount(0);
		 try {
				courseVOs=controller.getMyCourse();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				ExceptionHandler.networkException();
			}
		 for(CourseVO course:courseVOs){
			 String[] array=new String [5];
			 array[0]=course.getCourseId();
			 array[1]=course.getCourseName();
	 		 array[2]=course.getDepartmentID();
			 array[3]=course.getCourseTime();
			 array[4]=String.valueOf(course.getCredit());
			 
			 tableModel.addRow(array);
			 table.invalidate();
			 table.updateUI();
	      }
	} 
	
	
	public void setTabbedPane(MyTabbedPane tabbedPane){
		this.tabbedPane=tabbedPane;
	}

	
}
